package com.example.tass.controller;

import cn.tass.SRJ1933.devices.inf.TassSignServerApi;
import cn.tass.SRJ1933.devices.params.response.asym.AsymOperationResponse;
import cn.tass.exceptions.TAException;
import com.example.tass.common.ResultMessage;
import com.example.tass.domain.MyAsymDecryptRequest;
import com.example.tass.domain.MyAsymEncryptRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 3.6 非对称加解密
 */
@RestController
public class AsyEncAndDecTest_3_6 {

    private static String printFormat = "错误码[%d]\n错误消息[%s]\n接口结果： %s\n";
    private static final Logger logger = LoggerFactory.getLogger(AsyEncAndDecTest_3_6.class);
    private TassSignServerApi signServerApi;

    public AsyEncAndDecTest_3_6(TassSignServerApi signServerApi) {
        this.signServerApi = signServerApi;
    }

    // 3.6.1 公钥加密（内部密钥） 【该接口通过内部非对称密钥或证书完成加密操作。】
    @GetMapping(value = "/asymEncrypt")
    public ResultMessage asymEncrypt(@RequestBody MyAsymEncryptRequest request) {
        try {
            AsymOperationResponse response = signServerApi.asymEncrypt(request.getKeyOrCertId(), request.getKeyOrCertIdType(), request.getData(), request.getEncFormat());
            if (response.getCode() != 0) {
                logger.error(response.getMsg());
                return ResultMessage.newFailureResult(response.getMsg());
            } else {
                System.out.println("生成对称密钥（生成对称密钥 – 明文返回）： " + String.format(printFormat, response.getCode(), response.getMsg(), response.getEncrypt()));
                return ResultMessage.newSuccessResult(response.getEncrypt());
            }
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.6.2私钥解密（内部密钥） 该接口通过内部非对称密钥完成解密操作。

    // 3.6.2.1私钥解密，内部密钥（明文返回）
    @GetMapping(value = "/asymDecrypt")
    public ResultMessage asymDecrypt(@RequestBody MyAsymDecryptRequest request) {
        try {
            AsymOperationResponse response = signServerApi.asymDecrypt(request.getKeyId(), request.getKeyIdType(), request.getKeyAccessCode(), request.getEncrypt(), request.getEncFormat());
            System.out.println("生成对称密钥（生成对称密钥 – 明文返回）： " + String.format(printFormat, response.getCode(), response.getMsg(), response.getData()));
            return ResultMessage.newSuccessResult(response.getData());
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }

    // 3.6.2.2私钥解密，内部密钥（转加密后返回）

    @GetMapping(value = "/asymDecrypt_2")
    public ResultMessage asymDecrypt_2(@RequestBody MyAsymDecryptRequest request) {
        try {
            AsymOperationResponse response = signServerApi.asymDecrypt(request.getKeyId(), request.getKeyIdType(),
                    request.getKeyAccessCode(), request.getEncrypt(), request.getEncFormat(), request.getTransEncKeyId(), request.getKeyAccessCode(), request.getTransEncMode(),
                    request.getTransEncPadMode(), request.getTransEncIv());
            System.out.println("生成对称密钥（生成对称密钥 – 明文返回）： " + String.format(printFormat, response.getCode(), response.getMsg(), response.getData()));
            return ResultMessage.newSuccessResult(response.getData());
        } catch (TAException e) {
            throw new RuntimeException(e);
        }
    }
    
    // 3.6.3公钥加密（外部公钥和证书）
    // TODO

}
